package com.distribute.application.api.rest.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.framework.model.Commodity;
import com.distribute.framework.model.Headline;
import com.distribute.framework.model.Member;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
@RestController(value="api.ArticlesController")
public class ArticlesController extends BaseApiController{
	/**
	 * 会员获取品牌头条列表信息
	 * @return
	 */
	@RequestMapping("/api/articles/getAList.do")
	public @title("会员获取品牌头条列表信息") Protocol getAList(
			@title("品牌编号") Long banrdId,
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		
		protocol.setPage(Headline.where.page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<Headline>(){
			@Override
			public Map format(Headline t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员获取品牌头条详情信息
	 * @return
	 */
	@RequestMapping("/api/articles/getADetails.do")
	public @title("会员获取品牌头条详情信息") Protocol getADetails(
			@title("文章编号") Long aId
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Headline headline=Headline.where.id(aId).get();
		if (headline!=null) {
			Map json=headline.toJSON();
			json.put("goods", Commodity.where.id(headline.getCommod()).get().toJSON());
			String[] ids=headline.getRelatedArticles()==null||headline.getRelatedArticles().equals("")?new String[]{}:headline.getRelatedArticles().split(",");
			List list=new ArrayList();
			if (ids.length>0) {
				for (String string : ids) {
					if (string!=null&&!string.equals("")) {
						list.add(Headline.where.id(Long.valueOf(string)).get().toJSON());
					}
				}
			}
			json.put("list", list);
			protocol.set("obj", json);
		}else {
			protocol.set("obj", null);
		}
		return protocol;
	}
	/**
	 * 会员获取滚动品牌头条
	 * @return
	 */
	@RequestMapping("/api/articles/getScorrlList.do")
	public @title("会员获取滚动品牌头条") Protocol getScorrlList(
			@title("品牌编号") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
			
		}
		protocol.setList(Headline.where.merchant(banrdId).scroll((short)1).list(-1), new Protocol.BeanFormat<Headline>(){
			@Override
			public Map format(Headline t) {
				Map json = new HashMap();
				json.put("title", t.getTitle());
				return json;
			}
		});
		return protocol;
	}
}
